﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using ControlDePeso.DataSet;
using System.Configuration;

namespace ControlDePeso
{
    /// <summary>
    /// Class that handles the database layer.
    /// </summary>
    class DataBase
    {
        // connection string from the config file.
        OleDbConnection _connection = new OleDbConnection(Properties.Settings.Default.ControlDePesoConnectionString);
        
        /// <summary>
        /// Executes a statement with ExecuteNonQuery function.
        /// </summary>
        /// <param name="command">Command to execute.</param>
        /// <returns>Returns the number of rows affected.</returns>
        public int ExecuteNonQuery(string command)
        {
            OleDbCommand oleDbCommand = new OleDbCommand(command, _connection);
            OleDbDataAdapter oldbAdapter = new OleDbDataAdapter(oleDbCommand);

            _connection.Open();

            int rows = oleDbCommand.ExecuteNonQuery();

            return rows;
        }

        /// <summary>
        /// Executes a statement with ExecuteScalar function.
        /// </summary>
        /// <param name="command">Command to execute.</param>
        /// <returns>Returns the first column of the first row in the result set returned by the query.</returns>
        public object ExecuteScalar(string command)
        {
            OleDbCommand oleDbCommand = new OleDbCommand(command, _connection);
            OleDbDataAdapter oldbAdapter = new OleDbDataAdapter(oleDbCommand);

            _connection.Open();

            object rows = oleDbCommand.ExecuteScalar();

            return rows;
        }

        /// <summary>
        /// Gets the historical weight of a person.
        /// </summary>
        /// <param name="command">Command to execute.</param>
        /// <returns><see cref="PesoHistorialDataSet.xsd"/> with the historical information.</returns>
        public PesoHistorialDataSet GetHistoricalDataSet(string command)
        {
            OleDbCommand oleDbCommand = new OleDbCommand(command, _connection);
            OleDbDataAdapter oldbAdapter = new OleDbDataAdapter(oleDbCommand);

            _connection.Open();

            PesoHistorialDataSet pesoHistorialData = new PesoHistorialDataSet();
            oldbAdapter.Fill(pesoHistorialData.PesoHistorial);

            return pesoHistorialData;
        }
    }
}
